package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.NursingProjectPageQueryDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 分页条件查询护理项目
     *
     * @param dto
     * @return
     */
    @Override
    public PageBean<NursingProjectVo> pageQuery(NursingProjectPageQueryDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

        Page<NursingProjectVo> page = nursingProjectMapper.pageQuery(dto);

        return PageBean.of(page,NursingProjectVo.class);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDto
     */
    @Override
    public void createNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        //TODO  以后要处理的创建人和修改人
        nursingProject.setCreateBy(1671403256519078239L);
        nursingProject.setUpdateBy(1671403256519078239L);

        nursingProjectMapper.insert(nursingProject);

    }

    /**
     * 根据ID查询护理项目
     *
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo findNursingProjectById( Long id) {
        NursingProject nursingProject = nursingProjectMapper.findById(id);
        NursingProjectVo nursingProjectVo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return nursingProjectVo;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProjectDto
     * @return
     */
    @Override
    public void updateNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);

        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setCreateBy(1671403256519078239L);

        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    /**
     * 启用、禁用护理项目
     *
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        NursingProject nursingProject = NursingProject.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateBy(1671403256519078239L) //TODO
                .build();

        nursingProjectMapper.updateNursingProject(nursingProject);
    }

    /**
     * 删除护理项目
     *
     * @param id
     */
    @Override
    public void deleteNursingProject(Long id) {
//        1.先判断数据是否合法（以防查不到）
        NursingProject nursingProject = nursingProjectMapper.findById(id);

//        避免空指针异常
        if(nursingProject == null) {
            throw  new RuntimeException("查询不到对应护理项目，请求参数非法,");
        }
//        项目启用状态不可删除
        if(nursingProject.getStatus() == 1){
            throw  new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }

//        2.判断后才可进行删除
        nursingProjectMapper.deleteById(id);
    }

    /**
     * 查询所有护理项目
     *
     * @return
     */
    @Override
    public List<NursingProjectVo> selectAll() {

        List<NursingProject> nursingProjects = nursingProjectMapper.list();

        List<NursingProjectVo> nursingProjectVos = BeanUtil.copyToList(nursingProjects, NursingProjectVo.class);

        return nursingProjectVos;

    }


}
